from random import sample
from math import ceil, floor
from lotto.const import GAME, SIZE


def rng (mode):
    return range(1, SIZE[mode][1]+1);


def check (mode, date, quantity, numbers):
    results = [];
    for i in GAME[mode].objects.filter (date__gte = date)[:quantity]:
        lucky = [n for n in numbers if n in i.as_list ()];
        results.append ((i.id, i.date, i.as_list (), lucky));
    return results;


def randomize (mode, quantity = None):
    return sample (xrange(1, SIZE[mode][1]+1), quantity or SIZE[mode][0]);


def generate (mode):
    r = sample (xrange(1, SIZE[mode][1]+1), SIZE[mode][0]);
    r.sort ();
    for l in GAME[mode].objects.all ():
        l.as_list ().sort ();
        if r == l:
            return [];
    return r;


def mapsg (mode, month, year):
    if int(month) > 0:
        return GAME[mode].objects.filter (date__month = month, date__year = year).order_by ('-id');
    else:
        return GAME[mode].objects.filter (date__year = year).order_by ('-id');


def relationship (mode, date):
    dist = {};
    for i in xrange(0, SIZE[mode][2]+1):
        dist[str(i)+'/'+str(SIZE[mode][2]-i)] = 0;
    for l in GAME[mode].objects.filter (date__gte = date):
        p = [0, 0];
        for i in l.as_list ():
            p[i%2] += 1;
        dist[str(p[0])+'/'+str(p[1])] += 1;
    return dist;


def distrib (mode, date):
    dist = [0 for i in xrange(SIZE[mode][1])];
    for l in GAME[mode].objects.filter (date__gte = date):
        for i in l.as_list ():
            dist[i-1] += 1;
    return dist;


def distribten (mode, date):
    dist = [0 for i in xrange(int(ceil (SIZE[mode][1]/10.0)))];
    for l in GAME[mode].objects.filter (date__gte = date):
        for i in l.as_list ():
            dist[int(ceil(i/10.0))-1] += 1;
    return dist;


def distribuni (mode, date):
    dist = [0 for i in xrange(10)];
    for l in GAME[mode].objects.filter (date__gte = date):
        for i in l.as_list ():
            dist[i%10] += 1;
    return dist;


def distpai (mode, date, num):
    dist = [0 for i in xrange(SIZE[mode][1])];
    for l in GAME[mode].objects.filter (date__gte = date):
        if num in l.as_list ():
            for i in l.as_list ():
                dist[i-1] += 1;
    dist[num-1] = 0;
    return dist;


def pair (mode, date):
    dist = [0, 0];
    for l in GAME[mode].objects.filter (date__gte = date):
        for i in l.as_list ():
            dist[i%2] += 1;
    return dist;


def totalsum (mode, date):
    dist = [0 for i in xrange(sum(range(SIZE[mode][1]-SIZE[mode][2], SIZE[mode][1])))];
    for l in GAME[mode].objects.filter (date__gte = date):
        dist[sum(l.as_list ())] += 1;
    return dist;


def intervals (mode, date):
    dist = [list() for i in xrange(SIZE[mode][1])];
    j = 0;
    for l in GAME[mode].objects.filter (date__gte = date):
        for i in l.as_list ():
            dist[i-1].append (j - sum(dist[i-1]));
        j += 1;
    return [i[1:] for i in dist];


def intpai (mode, date, num):
    dist, days = [], [];
    j = 0;
    for l in GAME[mode].objects.filter (date__gte = date):
        if num in l.as_list ():
            dist.append (j - sum(dist));
            days.append (l.date);
        j += 1;
    return [(d, i) for d, i in zip(days, dist)];

